package jdbcForMe;

import jdbcForMe.exception.HaveNotCreateControllerException;
import jdbcForMe.handler.DBChanger;
import jdbcForMe.handler.DBSelector;
import jdbcForMe.mySetting.MyDBSetting;
import jdbcForMe.mySetting.MyFormSetting;
import jdbcForMe.mySetting.MyItemSetting;
import jdbcForMe.setting.DBSetting;
import jdbcForMe.setting.FormSetting;
import jdbcForMe.setting.ItemSetting;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBController {

    /**
     * 通过构造器直接将数据库的数据缓存到数据表
     * 打开控制器时加载
     * 关闭控制器时，如果数据表发生改变，则将原数据表 删除 后 重新创建 导入
     */

    private DBSetting dbSetting;
    private ItemSetting itemSetting;
    private FormSetting formSetting;

    public void setDbSetting(DBSetting dbSetting) {
        this.dbSetting = dbSetting;
    }

    public void setItemSetting(ItemSetting itemSetting) {
        this.itemSetting = itemSetting;
    }

    public void setFormSetting(FormSetting formSetting) {
        this.formSetting = formSetting;
    }

    /**
     * @param dbs 数据库的设置
     * @param is  数据表单体对象
     * @param fs  数据表表体对象
     */
    public DBController(DBSetting dbs, ItemSetting is, FormSetting fs) {
        dbSetting = dbs;
        itemSetting = is;
        formSetting = fs;
    }

    public DBController() {
    }

    /**
     * @return 返回一个数据库查询器对象
     * @throws SQLException                     SQLException
     * @throws HaveNotCreateControllerException HaveNotCreateControllerException
     */
    public DBSelector getSelector() throws SQLException {
        if (uncreated()) throw new HaveNotCreateControllerException("You may need to construct a Controller object");
        return new DBSelector(getConnection(this.dbSetting));
    }

    /**
     * @return 返回一个数据库修改器对象
     * @throws SQLException                     SQLException
     * @throws HaveNotCreateControllerException HaveNotCreateControllerException
     */
    public DBChanger getChanger() throws SQLException {
        if (uncreated()) throw new HaveNotCreateControllerException("You may need to construct a Controller object");
        return new DBChanger(getConnection(this.dbSetting));
    }

    /**
     * @param dbs 数据库DBSetting实现类
     * @return 数据库的Connection对象
     * @throws SQLException SQLException
     */
    private Connection getConnection(DBSetting dbs) throws SQLException {
        return DriverManager.getConnection(dbs.getUrl(), dbs.getUser(), dbs.getPasswd());
    }

    public void dispose() {
        formSetting.dispose();
        dbSetting = null;
        itemSetting = null;
        //包括 修改器 & 查询器 的dispose()
//        formSetting = null;
    }

    private boolean uncreated() {
        return dbSetting == null || itemSetting == null || formSetting == null;
    }


    static class Test {
//        public static void main(String[] args) {
//            MyDBSetting mdbs = new MyDBSetting();
//
//            MyItemSetting<Integer> mis01 = new MyItemSetting<>(1, "朵尔曼国际酒店(广州南站店)");
//            MyItemSetting<Integer> mis02 = new MyItemSetting<>(2, "壹加壹精品酒店(广州新白云国际机场店)");
//            MyItemSetting<Integer> mis03 = new MyItemSetting<>(3, "城市便捷酒店(广州汉溪长隆市桥地铁站店)");
//            MyItemSetting<Integer> mis04 = new MyItemSetting<>(4, "麗枫酒店(广州白云机场曙光路店)");
//            MyItemSetting<Integer> mis05 = new MyItemSetting<>(5, "广东空港大酒店");
//            MyFormSetting mfs = new MyFormSetting();
//            mfs.mix(mis01);
//            mfs.mix(mis02);
//            mfs.mix(mis03);
//            mfs.mix(mis04);
//            mfs.mix(mis05);
//            DBController dbc = new DBController(
//                    mdbs, mis01, mfs
//            );
//            try {
////                DBSelector dbs = dbc.getSelector();
////                System.out.println(dbs.select("hotelData"));
//                DBChanger dbChanger = dbc.getChanger();
//                System.out.println(mfs);
//                System.out.println("-----------");
////                dbChanger.update("test", mfs);
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }

        public static void main(String[] args) {
            MyDBSetting mdbs = new MyDBSetting();
            MyFormSetting mfs = new MyFormSetting();
            MyItemSetting<Integer> mis01 = new MyItemSetting<>(1, "朵尔曼国际酒店(广州南站店)");
            DBController dbc = new DBController(
                    mdbs, mis01, mfs
            );
            try {
                dbc.getChanger();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
